#-*-coding:utf-8-*-
'''
Created on 2011-1-16

@author: zhaoyh
'''
from sqlalchemy import Column, Integer,String
from db.meta import Base
from db.meta import DBSession,create_tables,drop_tables
class Company(Base):
    '''单位信息
    __tablename__ = 'ad_company'
    co_id = Column(String(100), primary_key=True)
    co_code = Column(String(100),nullable=False )
    co_name = Column(String(100),nullable=False )
    co_address = Column(String(100))
    co_manager = Column(String(100))
    co_phone = Column(String(50))
    co_website=Column(String(50))
    co_email=Column(String(50))
    co_notes=Column(String(500)
    '''
    __tablename__ = 'ad_company'
    columntype={'id':'co_id','code':'co_code'}
    co_id = Column(String(50), primary_key=True)
    co_code = Column(String(100),nullable=False )
    co_name = Column(String(100),nullable=False )
    co_address = Column(String(100))
    co_manager = Column(String(100))
    co_phone = Column(String(50))
    co_website=Column(String(100))
    co_email=Column(String(100))
    co_logo=Column(String(100))
    co_notes=Column(String(500))
    
    def __init__(self, **kwargs ):
        for k in kwargs.keys():
            setattr(self, k, kwargs[k])
        
    def __repr__(self):
        return "<Company('%s','%s', %s)>" % (self.co_code, self.co_name,self.co_logo)  
def init():
    """ 初始化单位信息"""
    edit_co=Company()
    edit_co.co_id='1'
    edit_co.co_code =''
    edit_co.co_name = ''
    edit_co.co_address =''
    edit_co.co_manager = ''
    edit_co.co_phone = ''
    edit_co.co_website=''
    edit_co.co_email=''
    edit_co.co_notes=''
    DBSession.add(edit_co) 
    DBSession.flush()
    DBSession.commit()
    return True
#@autocommit
def update(co_id,**attribute):
    """ 
         更新单位信息
    @param attribute:  字典 ,要更新的属性
    @return: true or false
    """
    try:
        edit_co=DBSession.query(Company).filter(Company.co_id==co_id).first()
        for k,v in  attribute.items():
            setattr(edit_co,k,v)
            #edit_co=attribute[key]  
        DBSession.flush()
        DBSession.commit()
        print edit_co
        return True
    except Exception,e:
        DBSession.rollback()
        print Exception,e
        return False
        raise

if __name__ == "__main__":
    drop_tables()
    create_tables()
    